from util.excel_util import ExcelUtil
from keywordselenium.actionMethod import ActionMethod
import sys

sys.path.append('E:/study/autostudy')

class KeywordCase:
    def run_main(self):
        self.action_method = ActionMethod()
        handle_excel = ExcelUtil('E:/study/autostudy/config/keyword.xls')
        case_lines = handle_excel.get_lines()
        if case_lines:
            for i in range(1,case_lines):
                is_run = handle_excel.get_col_value(i,3)
                if is_run == 'yes':
                    method = handle_excel.get_col_value(i, 4)
                    send_value = handle_excel.get_col_value(i, 5)
                    handle_value = handle_excel.get_col_value(i, 6)
                    except_result_mothod = handle_excel.get_col_value(i, 7)
                    except_result = handle_excel.get_col_value(i, 8)
                    self.run_method(method,send_value,handle_value)
                    if except_result != '':
                        except_value = self.get_except_resutl_value(except_result)
                        if except_value[0] == 'text':
                            result = self.run_method(except_result_mothod)
                            if except_value[1] in result:
                                handle_excel.wirte_value(i,'pass')
                            else:
                                handle_excel.wirte_value(i, 'fail')
                        elif except_value[0] == 'element':
                            result = self.run_method(except_result_mothod,except_value[1])
                            if result:
                                handle_excel.wirte_value(i, 'pass')
                            else:
                                handle_excel.wirte_value(i, 'fail')
                        else:
                            print('没有')
                    else:
                        print('预期结果为空')







     # 获取预期结果值
    def get_except_resutl_value(self,data):
        return  data.split('=')

    def run_method(self,method,send_value='',handle_value=''):

        method_value = getattr(self.action_method,method)
        if send_value == '' and handle_value != '':
            result = method_value(handle_value)
        elif send_value == '' and handle_value == '':
            result = method_value()
        elif send_value !='' and handle_value == '' :
            result = method_value(send_value)
        else:
            result = method_value(send_value,handle_value)
        return result



if __name__ == '__main__':
    test = KeywordCase()
    test.run_main()
